﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;

namespace MvcShop.Models.Repositories
{
    public class UserRepository:BaseRepositoryBase<User>,  IUserRepository<User>
    {
        public UserRepository()
        {
            this.EntitiesSetName = "Users";
        }

        #region IUserRepository<User> 成员

        public IEnumerable<User> Find(IEnumerable<int> values)
        {
            var query = this.EntitySet.Where(ContainsCriteria.ContainsExpression<User, int>(c => c.UserId, values)).ToList();
            return query;
        }

        public void Del(IEnumerable<int> values)
        {
            var query = this.EntitySet.Where(ContainsCriteria.ContainsExpression<User, int>(c => c.UserId, values)).ToList();
            foreach (var value in query)
            {
                this.dataContext.DeleteObject(value);

                //ObjectStateEntry ose= this.dataContext.ObjectStateManager.GetObjectStateEntry(value); 
            }
            this.SaveChanges();
        }

        public bool FindUserByEmail(string email)
        {
            var query = this.EntitySet.Count(c => c.Email == email);
            return query != 0 ? true : false;
        }

        public User GetUserByEmail(string email)
        {
            var query = this.EntitySet.FirstOrDefault(c => c.Email == email);
            return query;
        }

        public bool FindUserByName(string name)
        {
            var query = this.EntitySet.Count(c => c.UserName == name);
            return query != 0 ? true : false;
        }

        public User GetUserByName(string name)
        {
            var query = this.EntitySet.FirstOrDefault(c => c.UserName == name);
            return query;
        }

        public int GetUserIdByName(string name)
        {
            var query = this.EntitySet.Where(c => c.UserName == name).Select(c=>c.UserId).FirstOrDefault();
            return query;
        }

        public IList<User> FindAll(int page, int pageSize, Order orderExpr) 
        {
            var query = ToList(orderExpr.OrderFrom<User>(this.EntitySet).Skip((page - 1) * pageSize).Take(pageSize));
            return query;
        }

        #endregion
}

    public class RoleRepository : BaseRepositoryBase<Role>, IRoleRepository<Role>
    {
        public RoleRepository()
        {
            this.EntitiesSetName = "Roles";
        }

    }

    public class UserRoleRepository : BaseRepositoryBase<UserRole>, IUserRoleRepository<UserRole>
    {
        public UserRoleRepository()
        {
            this.EntitiesSetName = "UserRoles";
        }
    }

    public class ControllerActionRepository: BaseRepositoryBase<ControllerAction>, IControllerActionRepository<ControllerAction>
    {
        public ControllerActionRepository()
        {
            this.EntitiesSetName = "ControllerActions";
        }

        public void Del(IEnumerable<int> values)
        {
            var query = this.EntitySet.Where(ContainsCriteria.ContainsExpression<ControllerAction, int>(c => c.ControllerActionId, values)).ToList();
            foreach (var value in query)
            {
                this.dataContext.DeleteObject(value);
            }
            this.SaveChanges();
        }
    }

    public class ControllerActionRoleRepository : BaseRepositoryBase<ControllerActionRole>, IControllerActionRoleRepository<ControllerActionRole>
    {
        public ControllerActionRoleRepository()
        {
            this.EntitiesSetName = "ControllerActionRoles";
        }
    }

    public class UserInfoRepository : BaseRepositoryBase<UserInfo>, IUserInfoRepository<UserInfo>
    {
        public UserInfoRepository()
        {
            this.EntitiesSetName = "UserInfos";
        }

        public void Del(IEnumerable<int> values)
        {
            var query = this.EntitySet.Where(ContainsCriteria.ContainsExpression<UserInfo, int>(c => c.UserId, values)).ToList();
            foreach (var value in query)
            {
                this.dataContext.DeleteObject(value);
            }
            this.SaveChanges();
        }
    }
}
